#include"Structt.h"
#include"Menu.h"
#include"function.h"


Student* readFile( Student* s ,int n )  //读 
{
	FILE* fp;
	fopen_s(&fp,"zxcFile","rb");
	if(!fp)return s;
	fread(s,sizeof(Student ),n,fp);
	fclose(fp);
	return s;
}
void saveFile( Student* s,int n ) //写 
{
	FILE* fp;
	fopen_s(&fp,"zxcFile","wb");
		if(!fp)exit(0);
	fwrite(s,sizeof(Student ),n,fp);
	fclose(fp);
	return ;
}

void statistic(Student* s,int n)//un
{
	int a[n];
	memset(a,0,n);
	
	int max[n];
	memset(max,0,n);
	
	int count[n];
	memset(count,0,n);
	
	double aheight[n] ;
	memset(aheight,0,n);
	
	double aweight[n] ;
	memset(aweight,0,n);
	
	double achest_size[n] ;
	memset(achest_size,0,n);
	
	int a_height = 0;
	int a_weight = 0;
	int a_chest_size = 0;
	// max:now age

    for(int i = 0;i < n;i++){
    		
    	for(int m = 0;m<5;m++){
			
    		for(int z = m;z<5;z++){
    		     	
    			if(s[i].cc[m].age >= s[i].cc[z].age && s[i].cc[z].age!=0 &&s[i].cc[m].age!=0)max[i] =s[i].cc[m].age;
    			
    			if(s[i].cc[m].height >= s[i].cc[z].height && s[i].cc[z].height!=0 &&s[i].cc[m].height!=0)aheight[i] = s[i].cc[m].height;
    			
    			if(s[i].cc[m].weight >= s[i].cc[z].weight && s[i].cc[z].weight!=0 &&s[i].cc[m].weight!=0)aweight[i] = s[i].cc[m].weight;
    			
    			if(s[i].cc[m].chest_size >= s[i].cc[z].chest_size && s[i].cc[z].chest_size!=0 &&s[i].cc[m].chest_size!=0)achest_size[i] = s[i].cc[m].chest_size;
    			
    		 }
         }
     }
    
    // count:the number of the same age
    
    for(int i = 0;i<n&&max[i]!=0;i++ ){
    	count[i] = 1;
       
    	for(int m = 0;m<n&&max[m]!=0&&m!=i;m++){
    	
    		if(max[i] == max[m])  {
    			count[i]++;
    			count[m]++;
			}
    		
		}
	}  
	
    
    		
	 
	for(int i = 0;i<n&&max[i]!=0;i++){
		for(int j = 0;j<n&&max[j]!=0&&j!=i;j++){
			if(max[i] ==max[j] && strcmp( s[i].gender ,s[j].gender ) == 0 ){
				a_height  += aheight[i];
	            a_weight  += aweight[i] ;
	            a_chest_size  += achest_size[i];
			}
	 
		}
	}
	 
	for( int i =0;i<n&&max[i]!=0;i++){
    printf("\n\t%d\t%s\t%d\t%lf\t%lf\t%lf\n",max[i],s[i].gender,count[i],a_height,a_weight,a_chest_size );	
	}  
	             
	            
    printf("\n\tage\tgender\tnumber\taverheight\taverweight\taverchest_size");
        
	for( int i =0;i<n&&max[i]!=0;i++){
		
	    int b = 0;
	    
	    for( int m = 0;m<n&&max[m]!=0&&m!=i;m++){
	    	if(max[i] == max[m]) {
	    		a[i] = i ;
	    		b++;
			break ;
			}
			
			else a[i]=i;
		}
		
		if(b == 0)
		printf("\n\t%d\t%s\t%d\t%lf\t%lf\t%lf\n",max[i],s[i].gender,count[i],a_height/count[i],a_weight/count[i],a_chest_size/count[i] );		
	
		
	}
		
	
}


	//average about height, weight and chest_size in the same gender with the same age


void analyse(Student* s,int n)//un
{
	//rate of height, weight and chest_size by days and years
}

void change(Student* s,int n)
{
	int a ;
	printf("输入学号:");
	scanf("%d",&a);
	for(int i = 0;i < n ;i++)
	{
		if(s[i].sno == a)
		input2( &(s[i]) );
		
	}
	
	return ;
}

void del_fir(Student* s,int n)
{
	
	for(int i=0;i<n-1;i++){
	s[i]=s[i+1];
    }
	return ;
	
}

void del_las(Student* s,int n)
{

	int i=0;
	for(;i<n;i++)
	{
		if(s[i+1].sno == 0)break;
		
	}
	s[i]=s[i+1];

	return ;

}

void del_sno(Student* s,int n)
{
	
	int sno;
	printf("输入要删除信息的学生学号：");
	scanf("%d",&sno);
	int i=0;
	for(;i<n;i++){
		if(s[i].sno==sno)break;
		
	}
	for(;i<n;i++){
	s[i]=s[i+1];
	}
	
	return ;
	
}

void add(Student* s,int n)
{
	while(1){
		system("cls");
		printf("\t1.增加学生档案\n");
		printf("\t2.增加个人病例\n");
		printf("\t0.退出\n");
		printf("\t（0-1）\n");
		int option ;
		
		scanf("%d",&option);
		switch(option){
			case 0:
				return ;
				break;
				
		    case 1:
			    input1( s,n );
				break;	
				
			case 2:
			    add2( s,n );
				break;
			
			default :
			    continue;	
		}
	}
}

void add2(Student* s,int n)//cac
{
	int a;
	printf("输入学号：");
	scanf("%d",&a);
		for(int i = 0;i < n;i++)
	{
		if(a == s[i].sno )input2( &s[i] );
		else break;
	}
}

void input1(Student* s,int n)
{
	int i =0;
	while(1)
	{
		
		system("cls");
		printf("\t1.录入病例\n");
		printf("\t0.结束录入\n");
		printf("\t(0-1)\n");
		int option ;
		scanf("%d",&option);
		switch(option){
			case 0:
				return ;
				break;
			
			case 1:
				printf("\n\tsno:");
				scanf("%d",&s[i].sno);//
				
				printf("\n\tname:");
				fflush(stdin);
				gets(s[i].name);//
				
				
				printf("\n\tgender:");
				fflush(stdin);
				gets(s[i].gender);//
				input2(&s[i]);//
				i++;
				break;
				
			default :
			    continue;	
			    
			system("pause");   
		}
	}
    
	return ;
}

void input2 (Student* s)
{
	int i = 0;
	while(1)
	{
		system("pause");
		system("cls");
		printf("\t1.录入个人病例\n");
		printf("\t0.结束录入\n");
		printf("\t(0-1)\n");
		int option ;
		scanf("%d",&option);
		switch(option){
			case 0:
				return ;
				break;
			
			case 1:
				if (i>=5){
					printf("录入失败，请及时清理！");
					return ;
				}
				input3(& ( (s->cc)[i] ) );
				i++;
				break;
			
			default :
			    continue;	
		}
	}
    
	return ;
	
}

void input3(Cas* s)
{
	fflush(stdin);
	printf("\n\n\tdepart:");	
	gets(s->depart);
	
	printf("\n\tage:");
	scanf("%d",&s->age);
	
	printf("\n\tchest_size:");
	scanf("%lf",&s->chest_size);
	
	printf("\n\ttall:");
	scanf("%lf",&s->height);
	
	printf("\n\tweight:");
	scanf("%lf",&s->weight);
	 
	printf("\n\tdiagnose:");
	fflush(stdin);
    gets(s->diagnose );
     
    printf("\n\tyear:");
    scanf("%d",&s->year );
    
    printf("\n\tmonth:");
    scanf("\t%d",&s->month );
    
	printf("\n\tday:");
    scanf("\t%d",&s->day );
    
    return ;
}


Student* readfile(Student* s,int n)
{
	FILE* fp;
	fopen_s(&fp,"zxc_File","rb");
	if(!fp)exit(0);
	fread(s,sizeof(Student ),n,fp);
	fclose(fp);
	return s;
}

void savefile(Student* s,int n)
{
	FILE* fp;
	fopen_s(&fp,"zxc_File","wb");
	if(!fp)exit(0);
	fwrite(s,sizeof(Student ),n,fp);
	fclose(fp);
	return ;
}

void find_by_sno(Student* s,int n,int sno)
{
	for(int i = 0;i < n;i++){
		if( s[i].sno == sno )show1(&s[i]);
	}
}

void find_by_gender(Student* s,int n,char* gender)
{
	for(int i = 0;i < n;i++){
		if( strcmp(s[i].gender ,gender ) == 0 )show1(&s[i]);
	}
}

void find_by_name(Student* s,int n,char* name)
{
	for(int i = 0;i < n;i++){
		if( strcmp(s[i].name ,name ) == 0 )show1(&s[i]);
	}
}

void show1( Student* s )//一个学生
{
	if(  s->sno == 0 ){
			
			return ;
		}
		
	    printf("\n\tsno:%d",s->sno)	;
		
		printf("\n\tname:");
		puts(s->name);
		
		printf("\tgender:");
		puts(s->gender);	
		
	for(int i=0;i<5;i++){
		if(s->cc[i].age == 0 )return;
		else 
		{
		show2( &((s->cc)[i]) );
		
		printf("\n");
		}
	}
	return ;
}

void traversal(Student* s,int n)//所有学生
{
	for(int i = 0; i < n; i++){
		if(s[i].sno == 0) return;
	    show1(&s[i]);	
        
		system("pause");
	}
}



void show2(Cas* s)//一份记录
{

	printf("\n\tdepart:");
	puts(s->depart );
	
	printf("\tage:%d\n",s->age );
	
	printf("\ttall:%lf\n",s->height );
	
	printf("\tweight:%lf\n",s->weight );
	
	printf("\tchest_size:%lf\n",s->chest_size );
	
	printf("\tdiagnose:");
	puts(s->diagnose );
	
	printf("\tdate:%d.%d.%d",s->year ,s->month ,s->day );
	
	printf("\n");
	return ;
}



void deletE(Student* s,int n)
{
	while(1)
	{
		system("cls");
		printf("\n\t1.删除第一个学生");
		printf("\n\t2.删除最后一个学生");
		printf("\n\t3.删除指定学号的学生");
		printf("\n\t0.退出");
		printf("\n\t(0-3)\n");
		
		int option ;
		scanf("%d",&option);
		switch(option)
		{
			case 0:
				return;
				break;
			
			case 1:
				del_fir( s,n );
				traversal( s,n );
				break;
				
			case 2:
				del_las( s,n );
				traversal( s,n );
				break;
				
			case 3:
				del_sno( s,n );
				traversal( s,n );
				break;
				
			default:
				continue;
		}
	}
	return ;
}